package com.sanjiahantang.youjiaclient.module.me.favorite.presenters;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseBean;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.goods.beans.GoodsListBean;
import com.sanjiahantang.youjiaclient.module.goods.models.IGoodsModel;
import com.sanjiahantang.youjiaclient.module.goods.models.impl.GoodsModel;
import com.sanjiahantang.youjiaclient.module.goods.views.impl.GoodsDetailActivity;
import com.sanjiahantang.youjiaclient.module.match.beans.MatchBean;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.FavoriteCaseView;
import com.sanjiahantang.youjiaclient.module.me.favorite.views.FavoriteGoodsView;
import com.sanjiahantang.youjiaclient.utils.FileUtils;
import com.sanjiahantang.youjiaclient.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PHONE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TYPE;

/**
 * Created by zy on 2017/9/5.
 */

public class FavoriteGoodsPresenter extends BaseMvpPresenter<FavoriteGoodsView> implements CallBack<GoodsListBean> {

    private IGoodsModel goodsModel;
    private QuickAdapter mAdapter;
    public FavoriteGoodsPresenter(){
        goodsModel = new GoodsModel();
    }

    public ListAdapter getAdapter(Context context) {
        return mAdapter = new QuickAdapter<MatchBean.Goods>(context, R.layout.item_goods_favorite) {
            @Override
            protected void convert(BaseAdapterHelper helper, final MatchBean.Goods item) {
                helper.setText(R.id.goods_name,item.getTitle());
                helper.setText(R.id.goods_price, String.format("¥ %.2f" , item.getBasePrice()));
                Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_rect_bg).into((ImageView) helper.getView(R.id.goods_image));
                helper.getView(R.id.goods_name).setTag(R.id.decode,item.getMgiId());
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context, GoodsDetailActivity.class);
                        intent.putExtra("goodsId", item.getMgiId());
                        context.startActivity(intent);
                    }
                });
            }
        };
    }

    public void loadData(Context context) {
        mView.showProgress();
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        map.put("roleType",0);
        param.put(KEY_PARAM,map);
        goodsModel.loadFavoriteList(context,param,this);
    }

    @Override
    public void onSuccess(GoodsListBean response, Context context, Object tag) {
        mView.hideProgress();
        if(mAdapter != null){
            mAdapter.clear();
            mAdapter.addAll(response.getData().getList());
        }
    }

    @Override
    public void onFailure(String message) {
        mView.hideProgress();
    }

    public void deleteCollectInfo(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(FavoriteCaseView.KEY_ROLE_TYPE,0);
        param.put(KEY_PHONE,SPUtils.get(context, SPConfig.PHONE,"").toString());
        param.put(KEY_TYPE,0);
        param.put(FavoriteCaseView.KEY_FAVORITE_ID,id);
        mView.showProgress();
        goodsModel.deleteCollectInfo(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                listener.onLoad(response);
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null) mView.hideProgress();
    }

}
